package depta.DOMTree;

import java.awt.image.renderable.RenderContext;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import org.lobobrowser.html.HtmlObject;
import org.lobobrowser.html.HtmlRendererContext;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.domimpl.HTMLDocumentImpl;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.HtmlParser;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleBrowserFrame;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.html2.HTMLCollection;
import org.w3c.dom.html2.HTMLDocument;
import org.w3c.dom.html2.HTMLElement;


public class TreeBuilder {

    public static void main(String[] args) throws Exception {
    	
    	new TreeBuilder().generateDOM("FinalDataIndex.txt");
        
    	String file = "25.html";
    	
    	HTMLDocument doc = new TreeBuilder().parse(new File(file));
    	
    }

	private HTMLDocument parse(File file) {

        UserAgentContext uacontext = new SimpleUserAgentContext();
		
        DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);
        
        return parse(file, builder);
	}

	private void generateDOM(String index) throws Exception{
		    	
        UserAgentContext uacontext = new SimpleUserAgentContext();
        
        HtmlPanel contextComponent = new SimpleBrowserFrame(null);
        
        HtmlRendererContext rContext = new SimpleHtmlRendererContext(contextComponent, uacontext);
        
//        DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);
  
        DocumentBuilderImpl builder = new DocumentBuilderImpl(rContext);
        
        String line = null;
        
        BufferedReader br = new BufferedReader(new FileReader(new File(index)));
        
        Map<Integer,HTMLDocumentImpl> map = new HashMap<Integer, HTMLDocumentImpl>();
        
        System.setOut(new PrintStream(new File("output.txt")));
        
        while ((line = br.readLine())!=null){
        	
        	String[] spl = line.split(",");
        	
            new TreeBuilder().parse(Integer.valueOf(spl[0]),spl[1],builder,map);
        	
            System.out.println(spl[0]);
        
        }
        
        br.close();
        
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream("DOMMap.serial"));
        
		out.writeObject(map);
		
		out.close();

		
	}

	private void parse(Integer index, String urlString, DocumentBuilder builder, Map<Integer, HTMLDocumentImpl> map) {
		
		Thread t = new Thread(new ParserRunnable(index, urlString, builder, map));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public HTMLDocument parse(File file, DocumentBuilder builder) {
		
		Map<File,HTMLDocument> map = new HashMap<File, HTMLDocument>(1);
		
		Thread t = new Thread(new ParserRunnable(file, builder, map));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return map.get(file);
	}
	
}
